package com.stb.shared.util;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;

public final class MapFactory 
{
	private MapFactory()
	{
	}
	
	/**
	 * Create a map from key value pairs 
	 * @param <K>
	 * @param <V>
	 * @param keyValues
	 * 
	 * @return unmodifiable map
	 */
	public static <K, V> Map<K, V> create(Object... keyValues)
	{
		Map<K, V> map = new HashMap<K, V>(keyValues.length / 2);
		return populateMap(map, keyValues);
	}

	@SuppressWarnings("unchecked")
	private static <V, K> Map<K, V> populateMap(Map<K, V> map, Object... keyValues) 
	{
		for (int i = 0; i < keyValues.length; i+=2 )
		{
			map.put((K)keyValues[i], (V)keyValues[i+1]);
		}
		return Collections.unmodifiableMap(map);
	}

	/**
	 * Create a sorted map from specified keyValues
	 * @param <K>
	 * @param <V>
	 * @param keyValues
	 * @return
	 */
	public static <K, V> Map<K, V> createSorted(Object... keyValues)
	{
		Map<K, V> map = new TreeMap<K, V>();
		return populateMap(map, keyValues);
	}

	public static <K, V> Map<K, V> createLinked(Object... keyValues)
	{
		Map<K, V> map = new LinkedHashMap<K, V>(keyValues.length / 2);
		return populateMap(map, keyValues);
	}
}
